/**
*      author: Y_Yao
*      created: 2024-10-22 16:04:08
**/

#include<bits/stdc++.h>

using namespace std;

#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
int n = 0;
const int N = 15e4 + 7;
int dp[N];


struct node
{
    int head, tail,val;
}a[N];

int g(int l,int r,int head)
{
    int ans = 0;
    while(l <= r)
    {
        int mid = (l + r) >> 1;
        if(a[mid].tail < head) ans = mid , l = mid + 1;
        else r = mid - 1;
    }
    return ans;
}



bool comp(node a,node b)
{
    return a.tail < b.tail;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    for(int i = 1;i <= n;i++)
    {
        cin >> a[i].head >> a[i].tail;
        a[i].val = a[i].tail - a[i].head + 1;
    }

    sort(a + 1,a + 1 + n,comp);

    for(int i = 1;i <= n;i++)
    {
        int head = a[i].head;
        dp[i] = max(dp[i - 1],dp[g(1,i-1,head)] + a[i].val);
    }

    cout << dp[n] << endl;
    return 0;
}